<!DOCTYPE html>
<html>
<head>
    
	<meta name="mobile-web-app-capable" content="yes">
	<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
	<meta charset="UTF-8">
    <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
    <script src="../../script/jquery.min.js"></script>
	<script src="../../script/semantic/semantic.min.js"></script>
    
    <style>
        .hidden{
            display:none;
        }

        .disabled{
            opacity: 0.5;
            pointer-events: none;
        }
    </style>
</head>
<body>
    <div class="ui form">
        <div class="field">
            <i class="large user icon" style="margin-top: -0.2em; margin-right: 0.4em;"></i>
            <div class="ui mini horizontal statistic">
                <div id="connectedUsers" class="value">
                    0
                </div>
                <div class="label">
                    Connected Users
                </div>
            </div>
            <br>
            <small>Notes: Setting change will disconnect all clients and restart all the connections</small>
        </div>
        <div class="field">
            <div class="ui toggle checkbox">
                <input id="useUPNP" type="checkbox" name="upnp" onchange="toggleUPNP(this.checked);">
                <label>Enable UPnP on SFTP Server Port</label>
                <small>Aka Auto Port Forwarding. Disable this option if you are connecting within Local Area Network</small>
            </div>
        </div>
        <div class="field">
            <label>Listening Port</label>
            <div class="ui labeled input">
                <input id="listeningPort" type="number" placeholder="2022" min="22">
            </div>
        </div>
        <button class="ui black right floated button" onclick="updatePort();">Save Changes</button>
        <br><br>
        <div id="ok" class="ui secondary inverted green segment" style="display:none;">
            <i class="checkmark icon"></i> Setting Applied
        </div>
        <div id="error" class="ui secondary inverted red segment" style="display:none;">
            <i class="remove icon"></i> <span class="msg">Something went wrong</span>
        </div>
        <div class="ui divider"></div>
       
    </div>
    <br><br>
    <script>
        var serverAllowUPNP = false;
        
        function initConfigs(){
            $.get("../../system/storage/sftp/port", function(data){
                $("#listeningPort").val(data);
            });

            $.get("../../system/storage/sftp/upnp", function(data){
                if (data == true){
                    $("#useUPNP").parent().checkbox("set checked");
                }else{
                    $("#useUPNP").parent().checkbox("set unchecked");
                }
            });

            $.get("../../system/storage/sftp/users", function(data){
                $("#connectedUsers").text(data);
            })
        }
        initConfigs();

        function toggleUPNP(checked){
            $.ajax({
                url:"../../system/storage/sftp/upnp",
                data: {enabled: checked},
                method: "POST",
                success: function(data){
                    
                } 
            })
        }

        function updatePort(){
            var newPort = $("#listeningPort").val();
            if (isNaN(parseInt(newPort)) || parseInt(newPort) < 20 ){
                alert("Invalid port number entered");
                return
            }

            $.ajax({
                url:"../../system/storage/sftp/port",
                data: {port: newPort},
                method: "POST",
                success: function(data){
                    if (data.error !== undefined){
                        showError(data.error);
                    }else{
                        showOK();
                    }
                   
                } 
            })
        }

        function showOK(){
            $("#ok").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
        }

        function showError(msg){
            $("#error").find(".msg").text(msg);
            $("#error").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
        }
       
    </script>
</body>
</html>